<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validateTrigger="bind">
			<uni-forms-item name="id" label="编号">
				<uni-easyinput type="number" v-model="formData.id" />
			</uni-forms-item>
			<uni-forms-item name="src" label="图片">
				<cloud-image name="src" v-model="formData.src" :value="formData.src" />
			</uni-forms-item>
			<uni-forms-item name="isRecommend" label="是否推荐">
				<uni-data-checkbox v-model="formData.isRecommend" :localdata="formOptions.isRecommend_localdata" />
			</uni-forms-item>
			<uni-forms-item name="isShow" label="是否显示">
				<uni-data-checkbox v-model="formData.isShow" :localdata="formOptions.isShow_localdata" />
			</uni-forms-item>
			<uni-forms-item name="name" label="名称">
				<uni-easyinput v-model="formData.name" />
			</uni-forms-item>
			<uni-forms-item name="parent_id" label="父id">
				<uni-easyinput placeholder="父id" v-model="formData.parent_id" />
			</uni-forms-item>
			<uni-forms-item name="posid" label="排序，asc">
				<uni-easyinput type="number" v-model="formData.posid" />
			</uni-forms-item>

			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/cloud_goods_categories.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'cloud_goods_categories';

	function getValidator(fields) {
		let reuslt = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				reuslt[key] = validator[key]
			}
		}
		return reuslt
	}

	export default {
		data() {
			return {
				formData: {
					"id": null,
					"src": "",
					"isRecommend": 0,
					"isShow": 0,
					"name": "",
					"parent_id": "",
					"posid": null,
				},
				formOptions: {
					"isRecommend_localdata": [{
							"text": "否",
							"value": 0
						},
						{
							"text": "是",
							"value": 1
						}
					],
					"isShow_localdata": [{
							"text": "否",
							"value": 0
						},
						{
							"text": "是",
							"value": 1
						}
					]
				},
				rules: {
					...getValidator(["id", "src", "isRecommend", "isShow", "name", "parent_id", "posid"])
				}
			}
		},
		onLoad(e) {
			const id = e.id
			this.formDataId = id
			this.getDetail(id)
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 触发表单提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.submit().then((res) => {
					this.submitForm(res)
				}).catch((errors) => {
					uni.hideLoading()
				})
			},

			submitForm(value) {
				// 使用 clientDB 提交数据
				db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field(
					'goodsCount,id,src,isRecommend,isShow,name,parent_id,posid,recommendGoods').get().then((
					res) => {
						const data = res.result.data[0]
						if (data) {
							this.formData = data
						}
					}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>
